﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YiYiTong.Rocket.Helper
{
    /// <summary>
    /// redis操作类
    /// </summary>
    public class RedisService
    {
        public static IRedisClient GetRedis()
        {
            return RedisPoolManager.Instance.GetClient();
        }

        public static void PublishMessage(string channel, string msg)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                redis.PublishMessage(channel, msg);
            }
        }

        public static T Get<T>(string key)
        {
            var redis = RedisPoolManager.Instance.GetClient();
            var ret = redis.Get<T>(key);
            redis.Dispose();
            return ret;
        }
        public static bool Set(string key, object obj)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.Set(key, obj);
            }
        }
        public static bool Set(string key, object obj, TimeSpan expiresIn)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.Set(key, obj, expiresIn);
            }
        }
        public static bool Set(string key, object obj, DateTime exporesAt)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.Set(key, obj, exporesAt);
            }
        }
        public static void PrependItemToList(string key, string obj)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                redis.PrependItemToList(key, obj);
            }
        }

        public static string PopItemFromList(string listId)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.PopItemFromList(listId);
            }
        }

        /// <summary>
        /// 获取消息队列中的数量
        /// </summary>
        /// <param name="listId"></param>
        /// <returns></returns>
        public static long GetListCount(string listId)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.GetListCount(listId);
            }
        }

        public static void RemoveAll(IEnumerable<string> keys)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                redis.RemoveAll(keys);
            }
        }

        public static void Remove(string key)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                redis.Remove(key);
            }
        }

        public static List<string> SearchKeys(string pattern)
        {
            using (var redis = RedisPoolManager.Instance.GetClient())
            {
                return redis.SearchKeys(pattern);
            }
        }
    }
}